package com.googlecode.continuity.core.server.mvc.controller;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.googlecode.continuity.core.server.mvc.ContinuityModel;
import com.googlecode.continuity.core.server.service.UserService;

/**
 * EditUserFormController provides a few common methods and fields for form controllers which edit a user account.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public abstract class EditUserFormController extends SimpleFormController {
  /**
   * Logger for this class and subclasses
   */
  protected final Log logger = LogFactory.getLog(getClass());

  private UserService userService;

  /**
   * Returns a standard {@link com.googlecode.continuity.core.server.mvc.ContinuityModel} for general page display.
   *
   * @param request http request
   * @param command command object to be sent out
   * @param errors  error information
   * @return model for page display
   * @throws Exception
   */
  protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
    return new ContinuityModel();
  }

  /**
   * Creates a {@link com.googlecode.continuity.core.server.mvc.command.EditUserCommand} object.
   *
   * @param request http request
   * @return new {@link com.googlecode.continuity.core.server.mvc.command.EditUserCommand}
   * @throws ServletException
   */
  protected abstract Object formBackingObject(HttpServletRequest request) throws ServletException;

  /**
   * Returns this controller's user service reference.
   *
   * @return user service
   */
  public UserService getUserService() {
    return userService;
  }

  /**
   * Provides this controller with a reference to a user service.
   *
   * @param userService new user service
   */
  public void setUserService(UserService userService) {
    this.userService = userService;
  }
}
